import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import storage from '@/common/util/Storage.js';
import {
	VUEX_PARAMS,
	USER_INFO,
	ACCESS_TOKEN,
} from '@/common/data/constants.js'

const obj = {
	state: {
		// 持久化参数
		params: storage.getStorageSync(VUEX_PARAMS) || '',
		token: storage.getStorageSync(ACCESS_TOKEN) || '',
		userinfo: storage.getStorageSync(USER_INFO) || {},
		// 网络状态
		// networkType: networkType,
		// 服务器500
		serverError: false,

	},
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_USER_INFO: (state, userinfo) => {
			state.userinfo = userinfo
		},
		SET_PARAMS: (state, params) => {
			state.params = params
		},
	},
	actions: {
		set_userinfo({
			commit,
			state
		}, userinfo) {
			storage.setStorageSync(USER_INFO, userinfo)
			commit('SET_USER_INFO', userinfo)
		},
		set_token({
			commit,
			state
		}, token) {
			storage.setStorageSync(ACCESS_TOKEN, token)
			commit('SET_TOKEN', token)
		},
		set_params({
			commit,
			state
		}, params) {
			storage.setStorageSync(VUEX_PARAMS, params)
			commit('SET_PARAMS', params)
		},
	},
	getters: {
		token: state => state.token,
		userinfo: state => state.userinfo,
	}
}

const store = new Vuex.Store(obj);

export default store;
